﻿using System;
using System.Collections.ObjectModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using XamlingCore.Infrastructure.Repository;
using XamlingCore.Infrastructure.View;
using XamlingCore.Infrastructure.View.Implementation;

namespace XamlingCore.Infrastructure.SystemMessage
{
    public class SystemMessageManager : NotifyBase
    {
        public event EventHandler NewSystemMessage;

        private ObservableCollection<SystemMessagePackage> messages = new ObservableCollection
            <SystemMessagePackage>();

        private SystemMessagePackage currentSystemMessage;

        protected virtual void HandleRepositorySystemMessages(RepositoryBase repo, bool showErrors, bool showInfo)
        {
            repo.MessageCallback = (package) =>
            {
                LogSystemMessage(package,
                    package.IsError && showErrors || !package.IsError && showInfo
                    );
            };
        }

        public virtual void LogSystemMessage(SystemMessagePackage package, bool raiseNotification)
        {
            AddNewSystemMessage(package, raiseNotification);

            //TODO: Some actual logging!
        }

        public virtual void LogException(Exception ex, string title, bool raiseNotification)
        {
            var message = ParseExceptionToSystemMessage(ex, title);

            if (message != null)
            {
                LogSystemMessage(message, raiseNotification);
            }
        }

        public virtual void AddNewSystemMessage(SystemMessagePackage message)
        {
            AddNewSystemMessage(message, true);
        }

        public virtual void AddNewSystemMessage(SystemMessagePackage message, bool raiseNotification)
        {
            messages.Add(message);
            CurrentSystemMessage = message;

            if (NewSystemMessageCallack != null) NewSystemMessageCallack();

            if (NewSystemMessage != null)
            {
                NewSystemMessage(this, EventArgs.Empty);
            }
        }

        public SystemMessagePackage ParseExceptionToSystemMessage(Exception ex, string messageTitle)
        {
            if (ex != null)
            {
                var message = new SystemMessagePackage()
                {
                    Data = ex,
                    IsError = true,
                    Message = ex.ToString(),
                    Title = messageTitle
                };
                AddNewSystemMessage(message);
                return message;
            }
            return null;
        }

        public ObservableCollection<SystemMessagePackage> Messages
        {
            get { return messages; }
            set { messages = value; }
        }

        public SystemMessagePackage CurrentSystemMessage
        {
            get { return currentSystemMessage; }
            set
            {
                currentSystemMessage = value;
                OnPropertyChanged("CurrentSystemMessage");
            }
        }

        public Action NewSystemMessageCallack { get; set; }
    }
}
